package com.mzx.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mzx.demo.dao.CarDao;
import com.mzx.demo.pojo.Car;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
public class indexController {

    @Autowired
    CarDao carDao;

//    @CrossOrigin
    @RequestMapping("/demo")
    public Map<String, Object> demo(){
        List<Car> cars = carDao.findAllCar();
        Map<String,Object> carMap = new HashMap<>();
        carMap.put("data",cars);

        return carMap;
    }

    @CrossOrigin
    @RequestMapping("/saveCar")
    public String saveCar(@RequestBody Car car, HttpServletResponse response){

        car.setCtime(car.getCtime().split("\\.")[0]);
        if (carDao.saveCar(car)){
            return "200";
        }else {
            return "500";
        }
    }

    @RequestMapping("/delCar")
    public String delCar(@RequestBody String id){
        JSONObject idJson = JSON.parseObject(id);
        Integer idInt = Integer.parseInt(idJson.get("id").toString());
        if (carDao.delCar(idInt)){
            return "200";
        }else {
            return "500";
        }

    }


}
